package com.tanhua.dubbo.api.impl.mongo;

import com.tanhua.domain.mongo.Conclusion;
import com.tanhua.domain.mongo.Question;
import com.tanhua.domain.mongo.Questionnaire;
import com.tanhua.domain.mongo.UserEvaluate;
import com.tanhua.dubbo.api.mongo.SoulApi;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service(timeout = 200000)
public class SoulApiImpl implements SoulApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 返回所有的用户问卷
     *
     * @return
     */
    @Override
    public List<Questionnaire> findSoultest() {
        Query query = new Query();
        List<Questionnaire> questionnaireList = mongoTemplate.find(query, Questionnaire.class);
        return questionnaireList;
    }

    /**
     * 根据问题id找到问题
     *
     * @param qid
     * @return
     */
    @Override
    public Question findQuestionById(Integer qid) {
        Query query = new Query(Criteria.where("qid").is(qid));
        Question question = mongoTemplate.findOne(query, Question.class);
        return question;
    }

    /**
     * 根据用户id找到用户评估报告
     *
     * @param id
     * @return
     */
    @Override
    public UserEvaluate findUserEvaluateById(Long id) {
        Query query = new Query(Criteria.where("uid").is(id));
        UserEvaluate userEvaluate = mongoTemplate.findOne(query, UserEvaluate.class);
        return userEvaluate;
    }

    /**
     * 保存用户评估报告
     *
     * @param userEvaluate
     */
    @Override
    public void saveUserEvaluate(UserEvaluate userEvaluate) {
        mongoTemplate.save(userEvaluate);
    }

    /**
     * 修改用户评分,并且将用户的level+1
     *
     */
    @Override
    public void updateUserEvalute(UserEvaluate userEvaluate) {
        Query query = new Query(Criteria.where("uid").is(userEvaluate.getUid()).and("qid").is(userEvaluate.getQid()));
        Update update = new Update();
        update.set("lock", userEvaluate.getLock());
        update.set("score", userEvaluate.getScore());
        update.set("cid",userEvaluate.getCid());
        mongoTemplate.updateFirst(query, update, UserEvaluate.class);
    }

    /**
     * 根据任意一个题目id获取问卷号
     *
     * @param questionId
     * @return
     */
    @Override
    public Questionnaire findQuestionnaire(Long questionId) {
        //db.questionnaire.find({questionsId:27}),语句居然就这个...
        Query query = new Query(Criteria.where("questionsId").is(questionId));
        Questionnaire questionnaire = mongoTemplate.findOne(query, Questionnaire.class);
        return questionnaire;
    }

    /**
     * 根据cid找到对应的结论
     *
     * @param cid
     * @return
     */
    @Override
    public Conclusion findConclusionsById(Integer cid) {
        Query query = new Query(Criteria.where("cid").is(cid));
        Conclusion conclusion = mongoTemplate.findOne(query, Conclusion.class);
        return conclusion;
    }

    /**
     * 根据qid列表找到对应的问题集合
     *
     * @param questionsId
     * @return
     */
    @Override
    public List<Question> findQuestionsByIdList(List<Integer> questionsId) {
        Query query = new Query(Criteria.where("qid").in(questionsId));
        List<Question> questions = mongoTemplate.find(query, Question.class);
        return questions;
    }

    /**
     * //根据传递的cid获得其他相近的用户。
     *
     * @param cid
     * @param userId
     * @return
     */
    @Override
    public List<UserEvaluate> findUserEvaluateByCId(Integer cid, Long userId) {
        Query query = new Query(Criteria.where("cid").is(cid).and("uid").ne(userId));
        List<UserEvaluate> userEvaluates = mongoTemplate.find(query, UserEvaluate.class);
        return userEvaluates;
    }

    /**
     * 保存结论
     *
     * @param conclusion
     */
    @Override
    public void saveConclusion(Conclusion conclusion) {
        mongoTemplate.save(conclusion);
    }

    /**
     * 保存问题
     *
     * @param question
     */
    @Override
    public void saveQuestion(Question question) {
        mongoTemplate.save(question);
    }

    /**
     * 保存问卷
     *
     * @param questionnaire
     */
    @Override
    public void saveQuestionnaire(Questionnaire questionnaire) {
        mongoTemplate.save(questionnaire);
    }

    /**
     * 根据用户ID和对应的问卷ID找到对应的数据
     *
     * @return
     */
    @Override
    public UserEvaluate findUserEvaluateByIdAndQid(Long id, ObjectId qid) {
        Query query = new Query(Criteria.where("uid").is(id).and("qid").is(qid));
        UserEvaluate userEvaluate = mongoTemplate.findOne(query, UserEvaluate.class);
        return userEvaluate;
    }
}
